package com.songcha.sakura.provider

import android.content.Context
import com.alibaba.android.arouter.facade.annotation.Route
import com.songcha.sakura.MyApplication
import com.songcha.sakura.R
import com.songcha.sakura.util.MediaPlayerUtil
import com.songcha.library_business.provider.IOnWordStarProvider
import com.songcha.library_database_sakura.greendao.GreenDaoManager
import com.songcha.library_database_sakura.greendao.WordStarBean
import com.songcha.library_database_sakura.greendao.WordStarBeanDao

@Route(path="/sakura/WordStarImpl")
class IOnWordStarProviderImpl:IOnWordStarProvider {
    override fun onWordStar(word: String) {
        if(word.isBlank()) return
        MediaPlayerUtil.playMp3(com.songcha.sakura.MyApplication.getContext(), R.raw.magic)
        val dao=GreenDaoManager.getInstance().wordStarDao
        val bean=WordStarBean()
        bean.word=word.lowercase()
        dao.insertOrReplace(bean)
       // ToastUtil.show("已收藏单词")
    }

    override fun onWordUnStar(word: String) {
        if(word.isBlank()) return
        MediaPlayerUtil.playMp3(com.songcha.sakura.MyApplication.getContext(), R.raw.magic)
        try{
            val dao=GreenDaoManager.getInstance().wordStarDao
            val qb=dao.queryBuilder()
            qb.where(WordStarBeanDao.Properties.Word.eq(word.lowercase()))
            qb.buildDelete().executeDeleteWithoutDetachingEntities()
           // ToastUtil.show("已取消收藏单词")
        }catch (e:Exception){
            e.printStackTrace()
        }
    /*    val dao=GreenDaoManager.getInstance().wordStarDao
        val bean=WordStarBean()
        bean.word=word
        dao.delete(bean)*/
    }

    override fun getStarWordList(): MutableList<String> {
        val dao=GreenDaoManager.getInstance().wordStarDao
        val list=dao.loadAll()
        val wordList= mutableListOf<String>()
        for(item in list){
            wordList.add(item.word)
        }
        return wordList
    }

    override fun init(context: Context?) {

    }
}